/*
 * CS_REQ_INVEN_IDENTIFY.cpp
 *
 *  Created on: 2013. 10. 30.
 *      Author: mega@gamevil.com
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/Item.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_REQ_INVEN_IDENTIFY(User * pUser, ::Network::Packet * pPacket)
{
	PREPARE_PACKET;

	uint64_t itemSeq	= 0;
	uint8_t bagType		= 0;

	*pPacket >> itemSeq >> bagType;

	NLogic::Bag * pBag = pUser->GetBag(static_cast<INVEN_BAG_TYPE>(bagType));
	if (NULL == pBag)
	{
		DEBUGLOG("CS_REQ_INVEN_IDENTIFY : pBag is NULL!! bagtype=%d", bagType);
		return;
	}

	NLogic::Item * pItem = pBag->GetItem(itemSeq);
	if (NULL == pItem)
	{
		DEBUGLOG("CS_REQ_INVEN_IDENTIFY : pItem is NULL!! itemseq=%lld", itemSeq);
		return;
	}

	// 식별이 필요 없는 아이템
	if (1 != pItem->GetData().non_identity)
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_IDENTIFY_FAIL_NO_NEED);
		*pAck << itemSeq << bagType;
		pAck->MakePacket(CMD_SC_REQ_INVEN_IDENTIFY);
		pUser->Send(pAck);
		return;
	}

	// 식별 주문서가 있는지 확인
	NLogic::Bag * pBagMisc = pUser->GetBag(BAG_TYPE_MISC);
	if (NULL == pBagMisc)
	{
		DEBUGLOG("CS_REQ_INVEN_IDENTIFY : pBagMisc is NULL!!");
		return;
	}

	NLogic::Item * pScroll = pBagMisc->GetIdentifyScroll();

	// 식별 주문서 부족
	if (NULL == pScroll)
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_IDENTIFY_FAIL_SCROLL);
		*pAck << itemSeq << bagType;
		pAck->MakePacket(CMD_SC_REQ_INVEN_IDENTIFY);
		pUser->Send(pAck);
		return;
	}

	// 아이템을 식별 상태로 변경
	pItem->GetData().non_identity = 0;
	pItem->UpdateDatabaseItemInfo();

	// 식별 주문서 갯수 차감
	pScroll->GetData().quantity -= 1;

	if (pScroll->GetData().quantity <= 0)
	{
		pBagMisc->PopItem(pScroll->GetItemSeq());
		pScroll->DeleteDatabaseItemInfo(NLogic::Item::ItemStatus::EM_ITEM_STATUS_ITEM_USE);
	}
	else
	{
		pScroll->UpdateDatabaseItemInfo();
	}

	// 응답
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ACK_OK);
	*pAck << itemSeq << bagType;
	pAck->MakePacket(CMD_SC_REQ_INVEN_IDENTIFY);
	pUser->Send(pAck);
	return;
}

} /*NCommand*/ } /*ProjectZ*/
